package com.rrd.itext.xmlBean.layoutBean;

import java.util.List;

import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.rrd.itext.obj.ResouceObject;
import com.rrd.itext.utils.GlobalNames;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.StyleUtil;
import com.rrd.itext.utils.enumeration.AlignStyle;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.rrd.itext.xmlBean.layoutBean.iface.PositionElement;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Phrase")
public class XmlPhrase extends XmlFontStyle implements PdfElementRender, PdfElementParser<Object>, PositionElement {
	@XStreamAsAttribute
	private String align;
	@XStreamAsAttribute
	private String positionType;
	@XStreamAsAttribute
	private Float offsetX;
	@XStreamAsAttribute
	private Float offsetY;
	@XStreamAsAttribute
	private Float leading;
	@XStreamAlias("text")
	private XmlText text;
	@XStreamAlias("HTMLBlock")
	private XmlHtmlBlock htmlBlock;

	public boolean hasTextContent() {
		return text != null;
	}

	public int getAlign() {
		return AlignStyle.getAlign(this.align);
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getPositionType() {
		return positionType;
	}

	public void setPositionType(String positionType) {
		this.positionType = positionType;
	}

	public Float getOffsetX() {
		return offsetX;
	}

	public Float getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(Float offsetY) {
		this.offsetY = offsetY;
	}

	public void setOffsetX(Float offsetX) {
		this.offsetX = offsetX;
	}

	public XmlHtmlBlock getHtmlBlock() {
		return htmlBlock;
	}

	public void setHtmlBlock(XmlHtmlBlock htmlBlock) {
		this.htmlBlock = htmlBlock;
	}

	public XmlText getText() {
		return text;
	}

	public void setText(XmlText text) {
		this.text = text;
	}

	@Override
	public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
		cb.beginText();
		Font f = StyleUtil.createFont(this, builder.getStyleMap(), builder.getFontMap());
		if (this.hasTextContent()) {
			XmlText text = this.getText();
			String content = text.getText();
			if (content == null || "".equals(content)) {
				String name = text.getName();
				if (name == null) {
					return;
				}
				if (!builder.getResourceMap().containsKey(name)) {
					return;
				}
				content = ResouceObject.getDataContent(builder.getResourceMap(), name);
			}
			if (content == null) {
				return;
			}
			Phrase p = new Phrase(content, f);
			Float realLeading = this.getLeading() == null ? GlobalNames.DEFAULT_LEADING : this.getLeading();
			p.setLeading(realLeading);
			ColumnText.showTextAligned(cb, this.getAlign(), p, this.getOffsetX(), this.getOffsetY(), 0f);
			cb.endText();
		} else if (this.getHtmlBlock() != null) {
			List<Element> htmlElements = this.getHtmlBlock().parse(builder);
			Phrase p = new Phrase();
			p.setFont(f);
			p.setLeading(GlobalNames.DEFAULT_LEADING);
			p.addAll(htmlElements);
			ColumnText.showTextAligned(cb, this.getAlign(), p, this.getOffsetX(), this.getOffsetY(), 0f);
			cb.endText();
		}
	}

	public Float getLeading() {
		return leading;
	}

	public void setLeading(Float leading) {
		this.leading = leading;
	}

	@Override
	public Object parse(PDFBuilder builder) throws Exception {
		Font f = StyleUtil.createFont(this, builder.getStyleMap(), builder
				.getFontMap());
		if (this.hasTextContent()) {
			XmlText text = this.getText();
			String content = text.getText();
			if (content == null || "".equals(content)) {
				String name = text.getName();
				if (name == null) {
					return null;
				}
				if (!builder.getResourceMap().containsKey(name)) {
					return null;
				}
				content = ResouceObject.getDataContent(builder.getResourceMap(), name);
			}
			if (content == null) {
				return null;
			}
			Phrase p = new Phrase(content, f);
			return p;
		} else if (this.getHtmlBlock() != null) {
			List<Element> htmlElements = this.getHtmlBlock().parse(builder);
			Phrase p = new Phrase();
			p.setFont(f);
			p.addAll(htmlElements);
			return p;
		}
		return null;
	}

    @Override
    public Integer getWidth() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setWidth(Integer width) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public Integer getHeight() {
        return 0;
    }

    @Override
    public void setHeight(Integer height) {
        // TODO Auto-generated method stub
        
    }
}
